import json
import os.path

from langchain_core.prompts import PromptTemplate
from pydantic import Field

from infrastructure.constant.file import PROJECT_DIR

code_template = """import (
	"context"
	"tongquetai_server/activity_vk/domain/manage"
	"tongquetai_server/activity_vk/infrastructure/utils"
)
const (
	ActivityTemplateType manage.EnumTemplateType = "{{activity_template_type}}"
	ActivityVersion      manage.Version          = "{{activity_version}}"
	ActivityName                                 = "{{activity_name}}"
	ActType                                      = {{act_type}}
	BigType                                      = {{big_type}}
	BigTypeName                                  = "{{big_type_name}}"
)

func getActivityName(ctx context.Context) (activityName string, err error) {
	actTypeMap, err := utils.GetTemplateActTypeMap(ctx)
	if err != nil {
		return
	}
	activityName = actTypeMap[ActType]
	return
}
"""


def value_code_template_tool(
        activity_template_type: str = Field(..., description='活动模板类型'),
        activity_version: str = Field(..., description='活动版本'),
        activity_name: str = Field(..., description='活动名称'),
) -> str:
    template = PromptTemplate.from_template(
        template=code_template,
        template_format='jinja2',
    )
    type_data_map = get_act_type_data_map()
    type_data = type_data_map.get(activity_name)
    template_value = template.invoke({
        'activity_template_type': activity_template_type,
        'activity_version': activity_version,
        'activity_name': activity_name,
        'act_type': type_data.get('act_type'),
        'big_type': type_data.get('big_type'),
        'big_type_name': type_data.get('big_type_name')
    })
    return template_value.to_string()


def get_act_type_data_map() -> dict:
    info_map = {}
    file_path = os.path.join('activity_type_data.json')
    with open(file_path, 'r', encoding='utf-8') as f:
        result = json.load(f)
        data = result.get('data')
        big_type_map = {}
        for item in data.get('big_type_list', []):
            big_type_map[item['type']] = item['name']
        for item in data.get('type_list', []):
            info_map[item['name']] = {
                'act_type': item['type'],
                'act_type_name': item['name'],
                'big_type': item['big_type'],
                'big_type_name': big_type_map.get(item['big_type'], '')
            }
    return info_map


if __name__ == '__main__':
    result = value_code_template_tool('activity_template_type', 'activity_version', '秩序之殇')
    print(result)
